﻿using System;
using System.Collections.Generic;
using System.Text;
using BusinessFacade;
using PMS.Common;
using PMS.EntityQuery;
using System.Data;
using System.Data.SqlClient;

namespace EntityManage
{
    public class Sys_OrganizationManageExt : ISys_OrganizationHandlerExt
    {
        DataBase db = new DataBase();

        public Sys_OrganizationQuery GetRootOrganization()
        {
            Sys_OrganizationQuery _sys_OrganizationQuery = null;

            try
            {
                string SqlStr = " SELECT * FROM Sys_Organization WHERE Type = @Type";
                SqlParameter[] prams = new SqlParameter[1];
                prams[0] = new SqlParameter("@Type", Enums.OrgType.Branch);
                prams[0].Direction = System.Data.ParameterDirection.Input;

                DataTable dtblSys_Organization = db.RunSqlTab(SqlStr, prams);

                if (dtblSys_Organization != null)
                {
                    if (dtblSys_Organization.Rows.Count > 0)
                    {
                        _sys_OrganizationQuery = new Sys_OrganizationQuery();
                        if (dtblSys_Organization.Rows[0]["ID"] != System.DBNull.Value)
                        {
                            _sys_OrganizationQuery.ID = Convert.ToInt32(dtblSys_Organization.Rows[0]["ID"]);
                        }
                        if (dtblSys_Organization.Rows[0]["Parent_ID"] != System.DBNull.Value)
                        {
                            _sys_OrganizationQuery.Parent_ID = Convert.ToInt32(dtblSys_Organization.Rows[0]["Parent_ID"]);
                        }
                        _sys_OrganizationQuery.Code = Convert.ToString(dtblSys_Organization.Rows[0]["Code"]);
                        _sys_OrganizationQuery.Name = Convert.ToString(dtblSys_Organization.Rows[0]["Name"]);
                        if (dtblSys_Organization.Rows[0]["Sys_User_ID"] != System.DBNull.Value)
                        {
                            _sys_OrganizationQuery.Sys_User_ID = Convert.ToInt32(dtblSys_Organization.Rows[0]["Sys_User_ID"]);
                        }
                        if (dtblSys_Organization.Rows[0]["Type"] != System.DBNull.Value)
                        {
                            _sys_OrganizationQuery.Type = Convert.ToInt32(dtblSys_Organization.Rows[0]["Type"]);
                        }
                        if (dtblSys_Organization.Rows[0]["Assign_ID"] != System.DBNull.Value)
                        {
                            _sys_OrganizationQuery.Assign_ID = Convert.ToInt32(dtblSys_Organization.Rows[0]["Assign_ID"]);
                        }
                        if (dtblSys_Organization.Rows[0]["Status"] != System.DBNull.Value)
                        {
                            _sys_OrganizationQuery.Status = Convert.ToInt32(dtblSys_Organization.Rows[0]["Status"]);
                        }
                    }
                }
            }
            catch { }

            return _sys_OrganizationQuery;
        }

        public List<Sys_OrganizationQuery> GetChildByParentID(int parentId)
        {
            List<Sys_OrganizationQuery> result = new List<Sys_OrganizationQuery>() ;

            try
            {
                string SqlStr = " SELECT * FROM Sys_Organization WHERE Parent_ID = @ParentID and Status != 2";
                SqlParameter[] prams = new SqlParameter[1];
                prams[0] = new SqlParameter("@ParentID", parentId);
                prams[0].Direction = System.Data.ParameterDirection.Input;

                DataTable dtblSys_Organization = db.RunSqlTab(SqlStr, prams);

                if (dtblSys_Organization != null)
                {
                    if (dtblSys_Organization.Rows.Count > 0)
                    {
                        foreach(DataRow row in dtblSys_Organization.Rows) {
                            Sys_OrganizationQuery _sys_OrganizationQuery = new Sys_OrganizationQuery();
                            if (row["ID"] != System.DBNull.Value)
                            {
                                _sys_OrganizationQuery.ID = Convert.ToInt32(row["ID"]);
                            }
                            if (row["Parent_ID"] != System.DBNull.Value)
                            {
                                _sys_OrganizationQuery.Parent_ID = Convert.ToInt32(row["Parent_ID"]);
                            }
                            _sys_OrganizationQuery.Code = Convert.ToString(row["Code"]);
                            _sys_OrganizationQuery.Name = Convert.ToString(row["Name"]);
                            if (row["Sys_User_ID"] != System.DBNull.Value)
                            {
                                _sys_OrganizationQuery.Sys_User_ID = Convert.ToInt32(row["Sys_User_ID"]);
                            }
                            if (row["Type"] != System.DBNull.Value)
                            {
                                _sys_OrganizationQuery.Type = Convert.ToInt32(row["Type"]);
                            }
                            if (row["Assign_ID"] != System.DBNull.Value)
                            {
                                _sys_OrganizationQuery.Assign_ID = Convert.ToInt32(row["Assign_ID"]);
                            }
                            if (row["Status"] != System.DBNull.Value)
                            {
                                _sys_OrganizationQuery.Status = Convert.ToInt32(row["Status"]);
                            }

                            result.Add(_sys_OrganizationQuery);
                        }
                    }
                }
            }
            catch { }

            return result;
        }

        public string GetAllChildIdsByParentID(int parentId)
        {
            StringBuilder builder = new StringBuilder();

            try
            {
                string sql = "select * from Sys_Organization where Parent_ID = @Parent_ID";
                SqlParameter[] parms = new SqlParameter[]{ new SqlParameter("@Parent_ID", parentId) };
                DataTable dt = db.RunSqlTab(sql, parms);
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        builder.Append(row["ID"].ToString());
                        builder.Append(",");
                        builder.Append(GetAllChildIdsByParentID(int.Parse(row["ID"].ToString())));
                    }
                    builder = builder.Remove(builder.Length - 1, 1);
                }
            }
            catch { }

            return builder.ToString();
        }
    }
}
